package com.example.demo.threadTest;

import lombok.SneakyThrows;
 
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
/**
 * CompletableRunnable
 *
 * @author behappyto.cn
 * @date 2022/6/16 22:44
 */
public class CompletableRunnable {

    public static void main(String[] args) {
        //runAsync();
        //supplyAsync();
        //anyOf();
        completableFuture3();
    }
    public static  void runAsync() {
        // 默认 线程池
        CompletableFuture.runAsync(() -> {
            System.out.println("runAsync 使用示例");
        });
 
        // 自定义 线程池
        CompletableFuture.runAsync(() -> {
            System.out.println("runAsync 使用示例");
        },getPoolExecutor());
    }
 
    @SneakyThrows
    public  static void supplyAsync() {
        // 默认 线程池
        String supplyAsync1 = CompletableFuture.supplyAsync(() -> "runAsync 使用示例")
                .get();
        System.out.println(supplyAsync1);
 
        // 自定义 线程池
        String supplyAsync2 = CompletableFuture.supplyAsync(() -> "runAsync 使用示例")
                .get();
        System.out.println(supplyAsync2);
    }
 
    @SneakyThrows
    public static void anyOf() {
        // 运行 runAsync1
        CompletableFuture<Void> runAsync1 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync1 运行");
        });
        // 运行 runAsync2
        CompletableFuture<Void> runAsync2 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync2 运行");
        }, getPoolExecutor());
 
        // 等待任意线程执行完成继续
        Object join = CompletableFuture.anyOf(runAsync1, runAsync2)
                .join();
        System.out.println("join" + join);
    }
 
    @SneakyThrows
    public void allOf() {
        // 运行 runAsync1
        CompletableFuture<Void> runAsync1 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync1 运行");
        });
        // 运行 runAsync2
        CompletableFuture<Void> runAsync2 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync2 运行");
        }, getPoolExecutor());
 
        // 等待全部线程执行完成再继续
        Object join = CompletableFuture.allOf(runAsync1, runAsync2)
                .join();
        System.out.println(join);
    }
 
    /**
     * 自定义 线程池
     *
     * @return 线程池
     */
    private static ThreadPoolExecutor getPoolExecutor() {
        return new ThreadPoolExecutor(2, 10, 0
                , TimeUnit.SECONDS, new LinkedBlockingDeque<>(10));
    }

    @SneakyThrows
    private static void completableFuture3()   {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello";
        });
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "world";
        });
        // 设置 completableFuture.get() 获取到的值
        //completableFuture.complete("aaa");

         CompletableFuture.allOf(completableFuture, cf).join();
        System.out.println(completableFuture.get());
        String s = cf.get();
        System.out.println(s);
    }

}